﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class MenuSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(List<GridDataItem> rows);
        public event AcceptEvent OnAcceptEvent;

        public delegate void CancelEvent();
        public event CancelEvent OnCancelEvent;

        //Propeties
        [Browsable(true), Category("Custom")]
        public bool IsMultipleSelects { get; set; }

        [Browsable(true), Category("Custom")]
        public string Roles { get; set; }

        [Browsable(true), Category("Custom")]
        public bool IsClearGridAfterClose { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            MenuSearchGrid.AllowMultiRowSelection = IsMultipleSelects;

            if (!IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.MenuColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.MenuSearchGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                if (IsClearGridAfterClose)
                    ClearMenuItemsGrid();
            }
        }

        protected void btnMenuItemsOK_Click(object sender, EventArgs e)
        {
            List<GridDataItem> rows = new List<GridDataItem>();
            foreach (Telerik.Web.UI.GridDataItem selectedItem in MenuSearchGrid.SelectedItems)
            {
                rows.Add(selectedItem);
            }

            if (IsClearGridAfterClose)
                ClearMenuItemsGrid();

            OnAcceptEvent(rows);
        }

        protected void btnMenuItemsCancel_Click(object sender, EventArgs e)
        {
            if (IsClearGridAfterClose)
                ClearMenuItemsGrid();
            OnCancelEvent();
        }

        public void ClearMenuItemsGrid()
        {
            MenuSearchGrid.DataSource = new string[] { };
            MenuSearchGrid.DataBind();
        }

        protected void btnMenuSearch_Click(object sender, EventArgs e)
        {

            MenuSearchGrid.DataBind();
        }

        public void GetRoles(int parent, int roleId)
        {
            switch(Roles)
            {
                case "Master" :   GetMasterRoles(parent, roleId);                   
                    break;
                case "Admin": GetAdminRoles(parent, roleId);
                    break;
                case "User": GetUserRoles(parent, roleId);
                    break;
            }
        }

        public void GetMasterRoles(int parent, int roleId)
        {            
           MenuSearchGrid.DataSource =   GPSTrackingDBV2Custom.Data.DataRepository.MenuProvider.GetMenuByTypeSearch(parent,true,null,null,roleId);
        }

        public void GetAdminRoles(int parent, int roleId)
        {
            MenuSearchGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.MenuProvider.GetMenuByTypeSearch(parent, null, true, null, roleId);
        }

        public void GetUserRoles(int parent, int roleId)
        {
            MenuSearchGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.MenuProvider.GetMenuByTypeSearch(parent, null, null, true, roleId);
        }

    }
}